<template >
<h1 style="text-align: center;">用户登录</h1>
<van-form @submit="onSubmit">
  <van-cell-group inset>
    <van-field
      v-model="loginForm.userName"
      name="username"
      label="用户名"
      placeholder="请输入用户名"
      :rules="[{ required: true, message: '请填写用户名' }]"
    />
    <van-field
      v-model="loginForm.passWord"
      type="password"
      name="password"
      label="密码"
      placeholder="请输入密码"
      :rules="[{ required: true, message: '请填写密码' }]"
    />
  </van-cell-group>
  <div style="margin: 16px;">
    <van-button round block type="primary" native-type="submit">
      提交
    </van-button>
  </div>
</van-form>

</template>
<script setup>
import { reactive,getCurrentInstance} from 'vue';
import{useRouter} from 'vue-router';


//获取当前实例
const {proxy}=getCurrentInstance()
const router=useRouter()
const loginForm=reactive({
  userName:'',
  passWord:''
})

//提交登录请求
const onSubmit= async (values)=>{
  //在这里暂时没有设置复杂的校验规则
  // console.log(loginForm)
 const res=await proxy.$api.login(loginForm)
  //  console.log(res)
 if(res.code===10000){
  localStorage.setItem('pz_token',res.data.token)
  localStorage.setItem('pz_userInfo',JSON.stringify(res.data.userInfo))
  router.push('/')
 }
}
</script>
<style lang="less" scoped>
  
</style>